<template>
    <div class="layout" :style="{height:deviceHeight+'px'}">
        <div class="main">
          <p class="title">欢迎使用雄安租房</p>
          <!-- <router-link class="optrue" to="/center">确定</router-link> -->
          <p><a class="deal-search" @click="goLANDLORD"><img src="../assets/img/ico-deal.png" alt=""></a><span>我是房东</span></p>
          <p><a class="listing-search" @click="goTENANT"><img src="../assets/img/ico-listing.png" alt=""></a><span>我是租客</span></p>
        </div>
        <p class="footer">©2017 研耀(上海)信息科技有限公司</p>
    </div>
</template>
<script>
  import { TENANT_TOKEN, LANDLORD_TOKEN, deviceHeight, LANDLORD_INFO, xatongSingup, TENANT_INFO, zhifubaoSingup } from '../assets/js/config'
  import cache from '../assets/js/cache'
  export default {
    name: 'index',
    data () {
      return {
        deviceHeight,
        token: {
          landlord: '',
          renter: '',
          src: '',
          role: '',
          app_id: '',
          source: '',
          auth_code: '',
          scope: '',
          authcode: '',
          uid: ''
        }
      }
    },
    created () {
      // let urlQuery = this.$route.query
      this.src = cache.getQueryString('src')
      this.role = cache.getQueryString('role')
      this.app_id = cache.getQueryString('app_id')
      this.source = cache.getQueryString('source')
      this.auth_code = cache.getQueryString('auth_code')
      this.authcode = cache.getQueryString('src')
      this.uid = cache.getQueryString('authcode')
      this.token.renter = cache.get(TENANT_TOKEN)
      this.token.landlord = cache.get(LANDLORD_TOKEN)
      // 支付宝关联等录
      if (this.auth_code) {
        this.isAutoSign()
      }
      if (this.authcode) {
        if (this.role === 'TENANT') {
          this.xacheck()
        }
      }
      // if (this.src === 'alipay') {
      //   if (this.role === 'LANDLORD') {
      //     this.$router.push({path: '/login/LANDLORD', query: {app_id: this.app_id, source: this.source, scope: this.scope, auth_code: this.auth_code}})
      //   } else {
      //     this.$router.push({ path: '/login/TENANT', query: {app_id: this.app_id, source: this.source, scope: this.scope, auth_code: this.auth_code} })
      //   }
      // } else if (this.src === 'xiongan') {
      //   this.$router.push({ path: '/login/TENANT', query: {uid: this.uid, authcode: this.authcode} })
      // }
    },
    mounted () {
    },
    methods: {
      // 支付宝登录
      isAutoSign () {
        let myVue = this
        this.post({
          url: zhifubaoSingup,
          sendData: {authCode: myVue.auth_code, loginSrc: 'alipay', loginType: myVue.role},
          success (data) {
            console.log(data, 9999)
            if (data.code === '0') {
              if (myVue.role === 'TENANT') {
                // cache.set(TENANT_ACCOUNT, myVue.login.userName)
                cache.set(TENANT_TOKEN, data.token)
                cache.set(TENANT_INFO, JSON.stringify(data.userInfo))
                myVue.$router.push({ path: '/renter' })
              } else {
                // cache.set(LANDLORD_ACCOUNT, myVue.login.phone)
                cache.set(LANDLORD_TOKEN, data.token)
                cache.set(LANDLORD_INFO, JSON.stringify(data.userInfo))
                myVue.$router.push({ path: '/landlord' })
              }
            } else {
              if (myVue.role === 'TENANT') {
                myVue.$router.push({path: '/login/TENANT', query: {src: myVue.src, uid: data.uid}})
              } else {
                myVue.$router.push({path: '/login/LANDLORD', query: {src: myVue.src, uid: data.uid}})
              }
            }
          },
          failed (res) {
            if (myVue.role === 'TENANT') {
              myVue.$router.push({path: '/login/TENANT', query: {src: myVue.src, uid: res.uid}})
            } else {
              myVue.$router.push({path: '/login/LANDLORD', query: {src: myVue.src, uid: res.uid}})
            }
          }
        })
      },
      // 雄安登录，只是写好了，没有让测试测。。。只是写了一个流程，后来雄安就改了，没有定规则
      xacheck () {
        let myVue = this
        this.post({
          url: xatongSingup,
          sendData: {uid: myVue.uid, loginSrc: 'xion_an', loginType: 'TENANT', authCode: myVue.authcode},
          success (data) {
            if (data.code === '0') {
              cache.set(LANDLORD_TOKEN, data.token)
              cache.set(LANDLORD_INFO, JSON.stringify(data.userInfo))
              myVue.$router.push({ path: '/landlord' })
            } else {
              myVue.$toast({
                message: '没有权限，请联系管理员。',
                duration: 1000
              })
            }
          }
        })
      },
      goLANDLORD () {
        if (this.token.landlord) {
          this.$router.push({path: '/landlord'})
        } else {
          this.$router.push({path: '/login/LANDLORD'})
        }
      },
      goTENANT () {
        if (this.token.renter) {
          this.$router.push({path: '/renter'})
        } else {
          this.$router.push({path: '/login/LANDLORD'})
        }
      }
    }
  }
</script>
<style scoped>
  .layout {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    background: url("../assets/img/indebg.png") center center no-repeat;
    background-size: 100% 100%;
    -webkit-background-size: 100% 100%;
    overflow: hidden;
    position: relative;
  }
  .main {
    width: 90%;
    padding-bottom: 1.8rem;
    background: url("../assets/img/hangbg.png") center center no-repeat;
    -webkit-background-size: cover;
    background-size: cover;
    position: absolute;
    left: 50%;
    top: 50%;
    -webkit-transform: translate(-50%,-50%);
    -moz-transform: translate(-50%,-50%);
    -ms-transform: translate(-50%,-50%);
    -o-transform: translate(-50%,-50%);
    transform: translate(-50%,-50%);
  }
  .main > p {
    padding-top: .8rem;
  }
  .main > .title {
    height: 1rem;
    line-height: 1rem;
    padding-top: .96rem;
    font-size: .48rem;
    text-align: center;
    color: #404040;
  }
  .main > p > a {
    height: 2rem;
    width: 2rem;
    line-height: 2rem;
    border-radius: .1rem;
    margin: 0 auto;
    display: block;
    font-size: .4rem;
    text-align: center;
    color: #fff;
  }
  .main > p > span {
    display: block;
    margin: .3rem auto 0;
    text-align: center;
    color: #555;
    font-size: .32rem;
  }
  .footer {
    position: absolute;
    width: 100%;
    left:0;
    bottom:5%;
    text-align: center;
    color: #fff;
  }
</style>
